<template>
  <div class="container">
    <img class="logo" src="../assets/images/logo.png" alt="logo">
    <!-- 表单 -->
    <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginFormRef">
      <el-form-item  prop="username">
        <!-- 用户名 -->
        <el-input prefix-icon="el-icon-user"  v-model="loginForm.username">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <!-- 密码 -->
        <el-input prefix-icon="el-icon-key" type='password' v-model="loginForm.password">
        </el-input>
      </el-form-item>
      <el-form-item class="button-group">
        <el-button type="primary" @click="handleLogin">登录</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {login} from '../api/userinfo'
export default {


  data(){
    return {
      loginForm:{
        username:'admin',
        password:'123456'
      },
      loginRules:{
        username:[
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 16, message: '长度应为3-16个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度应为3-16个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods:{
    
    async handleLogin(){
      try {
        const res = await login({
          username:this.loginForm.username,
          password:this.loginForm.password
        })
        sessionStorage.setItem("token", res.data.token);
        sessionStorage.setItem('userinfo', res.data)
        this.$router.push({path:'/home'})
      } catch (err) {
        console.log(err);
      }
    },
    resetForm(){
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>
<style scoped>
.container{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.logo{
  width: 120px;
  height: 120px;
  position: relative;
  top: 50px;
  border-radius: 50%;
  box-sizing: border-box;
  border:  16px solid #FFF ;
  background: #FFF;
  box-shadow: 0 0 5px 5px #AAA;
}
.login-form{
  display: inline-block;
  width: 380px;
  background: #FFF;
  border-radius: 5px;
  padding-top: 100px;
  padding-left: 50px;
  padding-right: 50px;
}
.button-group{
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>